package com.uenpay.im.ui.old.activity.chat;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.adapter.FolderAdapter;
import com.uenpay.im.entities.SendFile;
import com.uenpay.util.app.base.BaseActivity;

import java.io.File;
import java.util.ArrayList;


/**
 * @author SL
 * @date 2014/7/23
 * @copyRight http://www.qiyunxin.com
 */
public class FolderListActivity extends BaseActivity {

    private TextView send_file_btn;
    private ListView lvFiles;
    private SendFile mSendFile;
    private FolderAdapter adapter = null;
    private String storage_directory_path = "";
    private File currentParent;
    private ArrayList<File> currentFiles = new ArrayList<File>();

    public void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_folder_list;
    }

    @Override
    public void initView(View view) {
        lvFiles = (ListView) this.findViewById(R.id.files);
        send_file_btn = (TextView) findViewById(R.id.title_textview);
        send_file_btn.setText(getResources().getString(R.string.send_file));
        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.send_file));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.choose_file));

        findViewById(R.id.title_right_layout).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mSendFile != null) {
                            Intent intent = new Intent();
                            intent.putExtra("send_file", mSendFile);
                            setResult(RESULT_OK, intent);
                            FolderListActivity.this.finish();
                        }
                    }
                });

        boolean sdCardExist = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            storage_directory_path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath();
        }

        try {
            File root = new File(storage_directory_path);
            if (root.exists()) {
                File[] tem = root.listFiles();
                for (int i = 0, len = tem.length; i < len; i++) {
                    if (tem[i].getName().startsWith(".")) {
                        continue;
                    }
                    currentFiles.add(tem[i]);
                }
                currentParent = root;
                inflateListView(currentFiles);

            }
        } catch (Exception e) {

        }

        lvFiles.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                                    int position, long id) {
                if (currentFiles.get(position).isFile()) {
                    mSendFile = (SendFile) lvFiles.getItemAtPosition(position);
                    adapter.setSelectedItem(position);
                    return;
                }
                File[] tem = currentFiles.get(position).listFiles();
                if (tem == null || tem.length == 0) {
                    IMApplication.showToast(getResources().getString(
                            R.string.the_path_no_file));
                } else {
                    currentParent = currentFiles.get(position);
                    currentFiles.clear();
                    for (int i = 0, len = tem.length; i < len; i++) {
                        if (tem[i].getName().startsWith(".")) {
                            continue;
                        }
                        currentFiles.add(tem[i]);
                    }
                    inflateListView(currentFiles);
                }

            }
        });

        //返回键点击
        findViewById(R.id.back_layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
    }

    private void goBack() {
        try {

            if (!currentParent.getCanonicalPath()
                    .equals(storage_directory_path)) {

                currentParent = currentParent.getParentFile();
                File[] tem = currentParent.listFiles();
                currentFiles.clear();
                for (int i = 0, len = tem.length; i < len; i++) {
                    if (tem[i].getName().startsWith(".")) {
                        continue;
                    }
                    currentFiles.add(tem[i]);
                }
                inflateListView(currentFiles);
            } else {
                FolderListActivity.this.finish();
            }
        } catch (Exception e) {
        }
    }

    /**
     * @param files
     */
    private void inflateListView(ArrayList<File> files) {

        ArrayList<SendFile> list = new ArrayList<SendFile>();

        for (int i = 0; i < files.size(); i++) {
            // if(files.get(i).getName().startsWith(".")) {
            // continue;
            // }

            SendFile sendFile = new SendFile();
            sendFile.fileName = files.get(i).getName();
            sendFile.filePath = files.get(i).getAbsolutePath();
            sendFile.fileSize = sendFile
                    .getFileSize(files.get(i).length() + "");

            if (files.get(i).isDirectory()) {
                sendFile.is_file = 0;
                list.add(0, sendFile);
                File file = files.get(i);
                files.remove(i);
                files.add(0, file);

            } else {
                sendFile.is_file = 1;
                list.add(sendFile);
            }

        }
        adapter = new FolderAdapter(list, FolderListActivity.this);
        lvFiles.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        goBack();
    }

}